*****Post-exit Coup Attempt Analysis***** (use AVP_base.dta!!!)

cd "C:\Users\Wonjun Song\Dropbox\Personalism Project\_beforeAR\BJPS\AVP"

*Archigos41.dta (to archigos41_gwf excel)
gen cowcode = ccode
drop ccode
recode cow (680=678) /* Yemen */
recode cow (680=678) if year==1968
sort cowcode year
rename leader archigos_leader

sort cowcode eoutdate
gen year=year(eoutdate)

sort cowcode year
gen leader = archigos_leader

*calculate tenure in days
gen tenure = eoutdate - eindate

sum tenure if tenure>1000
sum tenure if tenure>1095
sum tenure if tenure>365

gen atleast1=.
recode atleast1 (.=1) if tenure>365

gen atleast3=.
recode atleast3 (.=1) if tenure>1000

sort cowcode year
merge m:m cowcode year using addAV
keep if _merge==3
rename _merge _mergeGWF

keep if atleast3==1

*coup days (to ptcoupdate excel)
insheet using http://www.uky.edu/~clthyn2/coup_data/powell_thyne_coups_final.txt, clear
rename ccode cowcode
recode cow (680=678) /* Yemen */
recode cow (680=678) if year==1968 & month==8
sort cowcode year
rename country PT_country

gen coupdate = mdy(month, day, year)
format coupdate %d

*import base data (archigos41_gwf.dta)
import excel "C:\Users\Admin\Dropbox\Personalism Project\_beforeAR\BJPS\AVP\archigos41_gwf.xls", sheet("Sheet1") firstrow
save "C:\Users\Admin\Dropbox\Personalism Project\_beforeAR\BJPS\AVP\AVP_base.dta"
sort cowcode year

*GWF2015 for regime-end date (GWF2015tscs.dta)
rename ccode cowcode
xtset cowcode year

gen regime_enddate = date(gwf_enddate, "DMY")
format regime_enddate %d

merge m:m cowcode year using GWFtscs2015
drop if _merge==2

edit cowcode year archigos_leader gwf_country gwf_casename year eoutdate coupdate regime_enddate regimeend_datesort loutregimedie couphapp

gen regimeend_datesort = date(gwf_enddate, "DMY")

format regime_enddate %tdnn/dd/CCYY

gen loutregimedie =0
replace loutregimedie =1 if eoutdate==regime_enddate

*see if leader outdate is same as regime enddate
replace coupdate=regime_enddate if loutregimedie==1

*if leader end and regime end is equal, fill in the coupdate (65)
replace coupdate=regime_enddate if eoutdate==regime_enddate

*if current coupdate is AFTER regime collapse, fill in coupdate with regiem enddate (48)
replace coupdate=regime_enddate if coupdate>regime_enddate & coupdate!=.

*cases where coup attempt happened before regime enddate (43)
gen couphappened =0
replace couphappened =1 if coupdate<regime_enddate /* coup happened before regime collapse */

*fill in rest of them, except 12/31/2014 (82)
replace coupdate=regime_enddate if coupdate==. & regimeend_datesort!=20088

*find out censoring (60)
tab gwf_casename if coupdate==.
gen censoring=0
replace censoring=1 if coupdate==. & regimeend_datesort==20088 /* (60 of them) */

replace coupdate = date("12-31-2015","MDY") if  coupdate==.


tab gwf_country if regimeend_datesort==20088 /* Dec 31st 2014 */

replace coupdate=regime_enddate if regimeend_datesort!=20088 & coupdate==. & couphappened ==0 /* fill in regime enddate if regime enddate not Dec 31 2014 AND coupdate is missing*/

browse coupdate regime_enddate couphapp



***Calculate logged days and months
gen untilcoupend = coupdate - eoutdate
replace untilcoupend=. if untilcoupend<0
gen loguntil = log(1+untilcoupend)

gen logtenure = log(1+tenure)

gen coldwar=0
replace coldwar=1 if year<1990

*from temp1.dta
keep cowcode year gdpcap gdpcapl loggdp mad_gdppc mad_lgdppc mad_l1lgdppc mad_l5lgdppc llog_gdppc
merge m:m cowcode year using addAVP_GDP
sum gdpcap gdpcapl loggdp mad_gdppc mad_lgdppc mad_l1lgdppc mad_l5lgdppc llog_gdppc



***Analysis (AVP_base.dta)***

reg loguntil logtenure loggdp coldwar pers4
reg loguntil logtenure loggdp coldwar pers4, vce(robust)
reg loguntil logtenure loggdp coldwar pers4, vce(cluster cowcode)
reg loguntil logtenure loggdp coldwar pers4, vce(cluster gwf_casename)
reg loguntil logtenure loggdp coldwar pers4, vce(bootstrap, rep(1000))

*all leaders
reg loguntil logtenure loggdp coldwar pers4, vce(cluster gwf_casename)
avplot pers4, plotregion(lcolor(none) ilcolor(none) style(none)) mcolor(gs10) msize(medsmall) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scheme(lean2) ylabel(,nogrid) ///
rlopts(lpattern(solid) lcolor(red)) xtitle("e(Personalization index|X)") ytitle("e(Days until coup attempt (logged)|X )") ///
title("{bf:A.} All leaders (N=271)") name(av1, replace) 


*natural death
reg loguntil logtenure loggdp coldwar pers4 if exit=="Natural Death", vce(cluster gwf_casename)
avplot pers4, plotregion(lcolor(none) ilcolor(none) style(none)) mcolor(gs10) msize(medsmall) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scheme(lean2) ylabel(,nogrid) ///
rlopts(lpattern(solid) lcolor(red)) xtitle("e(Personalization index|X)") ytitle("e(Days until coup attempt (logged)|X)") ///
title("Natural death (N=59)") xlabel(-0.6(0.2)0.6) ylabel(-8(4)8) name(av2, replace) 

*foreign
reg loguntil logtenure loggdp coldwar pers4 if exit=="Foreign", vce(cluster gwf_casename)
avplot pers4, plotregion(lcolor(none) ilcolor(none) style(none)) mcolor(gs10) msize(medsmall) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scheme(lean2) ylabel(,nogrid) ///
rlopts(lpattern(solid) lcolor(red)) xtitle("e(Personalization index|X)") ytitle("e(Days until coup attempt (logged)|X)") ///
title("Foreign (N=6)") xlabel(-0.6(0.2)0.6) ylabel(-8(4)8) name(av5, replace) 

*irregular
reg loguntil logtenure loggdp coldwar pers4 if exit=="Irregular", vce(cluster gwf_casename)
avplot pers4, plotregion(lcolor(none) ilcolor(none) style(none)) mcolor(gs10) msize(medsmall) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scheme(lean2) ylabel(,nogrid) ///
rlopts(lpattern(solid) lcolor(red)) xtitle("e(Personalization index|X)") ytitle("e(Days until coup attempt (logged)|X)") ///
title("Irregular (N=118)") xlabel(-0.6(0.2)0.6) ylabel(-8(4)8) name(av3, replace) 

*regular
reg loguntil logtenure loggdp coldwar pers4 if exit=="Regular", vce(cluster gwf_casename)
avplot pers4, plotregion(lcolor(none) ilcolor(none) style(none)) mcolor(gs10) msize(medsmall) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scheme(lean2) ylabel(,nogrid) ///
rlopts(lpattern(solid) lcolor(red)) xtitle("e(Personalization index|X)") ytitle("e(Days until coup attempt (logged)|X)") ///
title("Regular (N=80)") xlabel(-0.6(0.2)0.6) ylabel(-8(4)8) name(av4, replace) 

*illness
reg loguntil logtenure loggdp coldwar pers4 if exit=="Retired Due to Ill Health", vce(cluster gwf_casename)
avplot pers4, plotregion(lcolor(none) ilcolor(none) style(none)) mcolor(gs10) msize(medsmall) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scheme(lean2) ylabel(,nogrid) ///
rlopts(lpattern(solid) lcolor(red)) xtitle("e(Personalization index|X)") ytitle("e(Days until coup attempt (logged)|X)") ///
title("Retired due to illness (N=8)") xlabel(-0.6(0.2)0.6) ylabel(-8(4)8) name(av6, replace) 

*Figure 5
graph combine av1 av2 av3 av4 av5 av6, iscale(*.7) xcommon ycommon col(2) xsize(2.5) ysize(3)
graph save Graph "C:\Users\Admin\Dropbox\Personalism Project\_beforeAR\BJPS\AVP\avplot.gph", replace
graph export "C:\Users\Admin\Dropbox\Personalism Project\_beforeAR\BJPS\AVP\avplot.pdf", as(pdf) replace


graph combine av1 av2 av3 av4, iscale(*.7) xcommon ycommon col(2) 

*five to appendix
graph combine av2 av3 av4 av5 av6, iscale(*.7) col(2) xsize(5) ysize(6)










*all leaders
reg loguntil logtenure loggdp coldwar pers4, vce(cluster gwf_casename)
avplot pers4, plotregion(lcolor(none) ilcolor(none) style(none)) mcolor(gs10) msize(medsmall) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scheme(lean2) ylabel(,nogrid) ///
rlopts(lpattern(solid) lcolor(red)) xtitle("e(Personalization index|X)") ytitle("e(Days until coup attempt (logged)|X)") ///
name(av1, replace) xlabel(-0.6(0.2)0.6) ylabel(-8(4)8)







**********Let's make table!
eststo clear
*all leaders
eststo: reg loguntil pers4 logtenure loggdp coldwar, vce(cluster gwf_casename)
estimates store avp1
*natural death
eststo: reg loguntil pers4 logtenure loggdp coldwar if exit=="Natural Death", vce(cluster gwf_casename)
estimates store avp2
*irregular
eststo: reg loguntil pers4 logtenure loggdp coldwar if exit=="Irregular", vce(cluster gwf_casename)
estimates store avp3
*regular
eststo: reg loguntil pers4 logtenure loggdp coldwar if exit=="Regular", vce(cluster gwf_casename)
estimates store avp4
*foreign
eststo: reg loguntil pers4 logtenure loggdp coldwar if exit=="Foreign", vce(cluster gwf_casename)
estimates store avp5
*illness
eststo: reg loguntil pers4 logtenure loggdp coldwar if exit=="Retired Due to Ill Health", vce(cluster gwf_casename)
estimates store avp6


coefplot avp1 avp2 avp3 avp4 avp5 avp6, bylabel(Personalization index) ||, keep(pers4) coeflabel(pers4 = `""Personalization" "index""') scheme(lean2) ///
xline(0, lcolor(red) lpattern(dash)) norecycle byopts(xrescale) legend(rows(2) order(2 "All leaders (N=271)" 4 "Natural death (N=59)" 6 "Irregular exit (N=118)" 8 "Regular exit (N=80)" 10 "Foreign (N=6)" 12 "Illness retire (N=8)") size(small) position(6)) ///
ciopts(recast(rcap) lpattern(dash) color(gs10)) mcolor(black) xsize(6) ysize(4) 





********************************************************************************


***GWF_Erica sample
tab gwf_leadername cowcode if gwf_leader_duration==.
edit gwf_leadername cowcode gwf_leader_duration year

/* strtrim(s) returns s with leading and trailing blanks removed */

replace gwf_leadername = strtrim(gwf_leadername)
charlist gwf_leadername
drop gwf_leaderid

***bring temp1.dta's persoanlism measure
keep xgwf_country xgwf_casename xgwf_caseid xgwf_case_duration xgwf_leadername xgwf_leaderid xgwf_leader_duration gwf_regimetype cowcode year pers4 coup_binary coup_succ coup_fail
save "C:\Users\Admin\Dropbox\Personalism Project\_beforeAR\BJPS\AVP\AVP.dta"
xtset cowcode year

*merge
merge m:m cowcode year using addAVP_Erica
xtset xgwf_leaderid year
browse xgwf_leaderid xgwf_leadername year xgwf_leader_duration

*first and last years in the panel
sort xgwf_leaderid xgwf_leader_duration
browse xgwf_leaderid xgwf_leader_duration

egen firstyr = min(xgwf_leader_duration), by(xgwf_leaderid)
egen lastyr = max(xgwf_leader_duration), by(xgwf_leaderid)

gen isfirst = cond(xgwf_leader_duration == firstyr, 1, .)
gen islast = cond(xgwf_leader_duration == lastyr, 1, .)

browse xgwf_leaderid xgwf_leadername xgwf_leader_duration firstyr lastyr isfirst islast coup_binary d
browse islast xgwf_leaderid xgwf_leader_duration if xgwf_leader_duration==lastyr

*years since leader last year
browse xgwf_leadername cowcode year xgwf_casename islast goneyrs
recode islast (.=0)

*years until next coup attempt
browse xgwf_leadername cowcode year xgwf_casename islast goneyrs coup_binary
gen nextcoup=.
gen nextcoupsucc=.

tsset cowcode year
edit cowcode xgwf_casename year islast nextcoup coup_binary nextcoupsucc coup_succ if islast==1
*also open tempt1.dta
tsset cowcode year
edit cowcode xgwf_casename year coup_binary coup_succ



*prepare variables
keep if islast==1
sort cowcode year

gen logtenure = log(1+xgwf_leader_duration)
gen loggoneyrs = log(1+goneyrs)


















*leaderid
egen leaderid = group(xgwf_leadername xgwf_caseid)
browse leaderid xgwf_leadername xgwf_casename xgwf_caseid cowcode year

*leaderduration
egen duration











*Archigos41.dta
gen cowcode = ccode
drop ccode
recode cow (680=678) /* Yemen */
sort cowcode year
rename leader archigos_leader

sort cowcode eoutdate
gen year=year(eoutdate)

sort cowcode year
gen leader = archigos_leader
rename year lastyear


*GWF_Erica.dta
sort cowcode year
gen gwf=1
gen leader=gwf_leadername



*attach archigos to GWF
merge m:m cowcode leader using Archigos41
browse cowcode year gwf_casename leader eindate eoutdate lastyear
drop if _merge==2
count

keep cowcode year gwf_casename leader eindate eoutdate lastyear _merge
export excel using "GWF_Erica_Archigos", firstrow(variables)

*excel version of archigos
gen firstyear=year(eindate)
drop if firstyear<1945
keep cowcode idacr leader eindate eoutdate lastyear
order cowcode idacr leader eindate eoutdate lastyear
export excel using "Archigos", firstrow(variables)












*Create duration and spell variables
egen ccode = group(cowcode)
gen byear = eindate				      	          
gen eyear = eoutdate				      	          
replace eyear =2015 if eyear==.
gen spell = (eyear-byear) + 1               	/*create variable for how long each regime lasted in power*/
expand spell								/*expands data set by making a copy of each case observations for the number of years in power*/
gen year = byear
sort cowcode year
bysort cowcode: replace year = year + _n -1    	      /*make year variable by adding the number of observations within ccode to the previous year*/
gen duration = year-(byear-1)
gen failure = 0
replace failure = 1 if year==edoutdate    & edoutdate~=.	/*create <failure> variable which is coded 1 only in the last year for leadid if failed*/
compress
browse cowcode idacr year archigos_leader year eindate eoutdate

***LeaderGWF.dta
count /* 4591 */
sum gwf_leaderid /* 532 leaders */
xtset gwf_leaderid year
quietly by gwf_leaderid:  gen dup = cond(_N==1,0,_n)

browse gwf_leaderid gwf_casename gwf_leadername dup
drop if dup>1
drop dup 

***SvolikLeader.dta
count
sum leadid
xtset leadid year
quietly by leadid:  gen dup = cond(_N==1,0,_n)
tab dup /*698 leaders*/
sort cabb
gen leadername=leader
sort leadername

***merge them
gen leadername = gwf_leadername
sort leadername
merge m:m leadername using SvolikLeader

browse cowcode gwf_country cabb year gwf_leadername leader startdate enddate _merge
keep cowcode gwf_country cabb year gwf_leadername leader startdate enddate _merge
export excel using "Fillin", firstrow(variables)


keep leadid ccode cabb leader birth death startdate enddate censoring
export excel using "Svolik", firstrow(variables) replace











*****use country-year*****
*GWFcy
xtset cowcode year

*SvolikLeadercy
gen cowcode = ccode
recode cow (680=678) /* Yemen */
sort cowcode year
gen svolik_leadername = leader

*merge
merge m:m cowcode year using SvolikLeadercy
tab _merge
browse cowcode gwf_country cabb year gwf_leadername svolik_leadername startdate enddate _merge
tab gwf_leadername if _merge==1
